﻿using MapleLeaf.Client;
using System.Windows;
using System.Windows.Controls;

namespace MapleLeaf.Client.Controls {

    public partial class NumberInputBox : UserControl {

        public double MinValue {
            get => (double)GetValue(MinValueProperty);
            set => SetValue(MinValueProperty, value);
        }

        public double MaxValue {
            get => (double)GetValue(MaxValueProperty);
            set => SetValue(MaxValueProperty, value);
        }

        public double StepValue {
            get => (double)GetValue(StepValueProperty);
            set => SetValue(StepValueProperty, value);
        }

        public double Value {
            get => (double)GetValue(ValueProperty);
            set => SetValue(ValueProperty, value);
        }

        private void AddClick(object sender, RoutedEventArgs e) {
            Value += StepValue;
        }

        private void SubClick(object sender, RoutedEventArgs e) {
            Value -= StepValue;
        }


        public NumberInputBox() {
            InitializeComponent();
        }

        static NumberInputBox() {

            MinValueProperty = DependencyProperty.Register("MinValue", typeof(double), typeof(NumberInputBox), new PropertyMetadata(0.0));
            MaxValueProperty = DependencyProperty.Register("MaxValue", typeof(double), typeof(NumberInputBox), new PropertyMetadata(100.0));
            StepValueProperty = DependencyProperty.Register("StepValue", typeof(double), typeof(NumberInputBox), new PropertyMetadata(1.0));
            ValueProperty = DependencyProperty.Register("Value", typeof(double), typeof(NumberInputBox), new PropertyMetadata(0.0, null, delegate (DependencyObject d, object v) {
                object result = (double)v > ((NumberInputBox)d).MaxValue
                    ? ((NumberInputBox)d).MaxValue
                    : (double)v < ((NumberInputBox)d).MinValue ? ((NumberInputBox)d).MinValue : v;
                return result;
            }));
        }

        public static readonly DependencyProperty MinValueProperty;

        public static readonly DependencyProperty MaxValueProperty;

        public static readonly DependencyProperty StepValueProperty;

        public static readonly DependencyProperty ValueProperty;
    }
}
